//
//  SelectView.m
//  Test
//
//  Created by 红沙尘 on 17/6/8.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "SelectView.h"

@implementation SelectView

- (void)addSelectStateChangeObserver:(id)obj {
    
    NSMutableDictionary *shareDict = [self shareDict];
    
    NSString *key = [NSString stringWithFormat:@"%p",obj];
    
    if (!shareDict[key]) {
        shareDict[key] = @"no";
        self.image = self.normalImage;
    } else {
        
        if ([shareDict[key] isEqualToString:@"yes"]) {
            self.image = self.selectedImage;
        } else {
            self.image = self.normalImage;
        }
    }
}

- (void)updateSelectedWithData:(id)obj {
    
    NSMutableDictionary *shareDict = [self shareDict];

    NSString *key = [NSString stringWithFormat:@"%p",obj];
    
    NSString *selected = shareDict[key];
    
    if (!selected) return;//如果数据源不在监听范围内就不往下执行了

    shareDict[key] = [selected isEqualToString:@"yes"] ? @"no" : @"yes";
    
    if ([shareDict[key] isEqualToString:@"yes"]) {
        self.image = self.selectedImage;
    } else {
        self.image = self.normalImage;
    }
}

- (void)removeSelectStateChangeObserver {
    NSMutableDictionary *shareDict = [self shareDict];
    [shareDict removeAllObjects];
}

- (NSMutableDictionary *)shareDict
{
    static NSMutableDictionary *shareDict ;
    static dispatch_once_t USERINFO_INIT ;
    dispatch_once(&USERINFO_INIT, ^{
        shareDict = [[NSMutableDictionary alloc] init];
    });
    
    return shareDict;
}

@end

